home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
By the Book
/
Mac C Primer V2
/
5.1 - FormEdit
/
FormEdit.c
next >
Wrap
C/C++ Source or Header
|
1992-02-10
|
24KB
|
1,143 lines
/************************************************************/
/* */
/* FormEdit Code from Chapter Five of */
/* */
/* *** The Macintosh Programming Primer *** */
/* */
/* Copyright 1990, Dave Mark */
/* */
/* This program demonstrates specific Mac programming */
/* techniques. */
/* */
/************************************************************/
/********************/
/* MENUs */
/********************/
#define APPLE_MENU_ID 400
#define A_ABOUT_ITEM 1
#define FILE_MENU_ID 401
#define F_NEW_ITEM 1
#define F_CLOSE_ITEM 2
#define F_QUIT_ITEM 3
#define EDIT_MENU_ID 402
#define E_UNDO_ITEM 1
#define E_CUT_ITEM 3
#define E_COPY_ITEM 4
#define E_PASTE_ITEM 5
#define E_CLEAR_ITEM 6
/********************/
/* Window Types */
/********************/
#define NIL_WINDOW 0
#define UNKNOWN_WINDOW 1
#define DA_WINDOW 2
#define FORM_WINDOW 3
/********************/
/* ALRTs */
/********************/
#define ABOUT_ALERT 400
#define ERROR_ALERT_ID 401
/********************/
/* Error STRs */
/********************/
#define NO_MBAR BASE_RES_ID
#define NO_MENU BASE_RES_ID+1
#define NO_WIND BASE_RES_ID+2
/********************/
/* TextEdit */
/********************/
#define TE_NAME_AREA 0
#define TE_MISC_AREA 1
#define TE_ENTER_KEY 3
#define TE_DELETE_CHAR 8
#define TE_TAB_CHAR 9
#define TE_CARRIAGE_RETURN 13
/************************/
/* General Defines */
/************************/
#define BASE_RES_ID 400
#define NIL_POINTER 0L
#define MOVE_TO_FRONT (WindowPtr)-1L
#define REMOVE_ALL_EVENTS 0
#define DRAG_THRESHOLD 30
#define WINDOW_HOME_LEFT 5
#define WINDOW_HOME_TOP 45
#define NEW_WINDOW_OFFSET 20
#define MIN_SLEEP 0L
#define NIL_MOUSE_REGION 0L
#define LEAVE_WHERE_IT_IS FALSE
#define WNE_TRAP_NUM 0x60
#define UNIMPL_TRAP_NUM 0x9F
#define SUSPEND_RESUME_BIT 0x0001
#define RESUMING 1
#define NIL_STRING "\p"
#define UNTITLED_STRING "\p<Untitled>"
#define VISIBLE TRUE
#define HOPELESSLY_FATAL_ERROR "\pGame over, man!"
/************************/
/* Useful Macros */
/************************/
#define TopLeft( myRect ) (* (Point *) &(myRect.top) )
#define BotRight( myRect ) (* (Point *) &(myRect.bottom) )
/************************/
/* Typedefs */
/************************/
typedef struct
{
WindowRecord w;
int wType;
ControlHandle vScroll;
TEHandle nameTE, miscTE, curTE;
} FormRecord, *FormPeek;
/************************/
/* Globals */
/************************/
Boolean gDone, gWNEImplemented, gInBackground;
EventRecord gTheEvent;
MenuHandle gAppleMenu,
gFileMenu,
gEditMenu;
int gNewWindowLeft = WINDOW_HOME_LEFT,
gNewWindowTop = WINDOW_HOME_TOP;
Rect gNameRect = { 3, 43, 19, 250 },
gMiscRect = { 22, 43, 150, 231 },
gScrollBarRect = { 22, 234, 150, 250 };
/************************/
/* Routines */
/************************/
void AdjustCursor( Point mouse, RgnHandle region );
void AdjustMenus( void );
void AdjustScrollBar( FormPeek form );
void CommonAction( ControlHandle control, short *amount );
void CreateWindow( void );
void DoActivate( WindowPtr window, Boolean becomingActive );
void DoCloseWindow( WindowPtr window );
void DoContentClick( WindowPtr window, Point mouse );
void DoIdle( void );
void DoTEKey( char c );
void DoUpdate( WindowPtr window );
void DrawForm( WindowPtr window );
void ErrorHandler( int stringNum );
void HandleAppleChoice( int theItem );
void HandleEditChoice( int theItem );
void HandleEvent( void );
void HandleFileChoice( int theItem );
void HandleMenuChoice( long int menuChoice );
void HandleMouseDown( void );
void MainLoop( void );
void MenuBarInit( void );
pascal Boolean NewClikLoop( void );
void StartTextEdit( FormPeek form );
void SwitchToNewArea( FormPeek form, int newArea );
void ToolBoxInit( void );
void TurnOffTextArea( FormPeek form, int whichArea );
void TurnOnTextArea( FormPeek form, int whichArea );
pascal void VActionProc( ControlHandle control, int part );
int WindowType( WindowPtr window );
/******************************** main *********/
main()
{
ToolBoxInit();
MenuBarInit();
MainLoop();
}
/*********************************** ToolBoxInit */
void ToolBoxInit()
{
InitGraf( &thePort );
InitFonts();
FlushEvents( everyEvent, REMOVE_ALL_EVENTS );
InitWindows();
InitMenus();
TEInit();
InitDialogs( NIL_POINTER );
InitCursor();
}
/*********************************** MenuBarInit */
void MenuBarInit()
{
Handle myMenuBar;
if ( ( myMenuBar = GetNewMBar( BASE_RES_ID ) ) == NIL_POINTER )
ErrorHandler( NO_MBAR );
SetMenuBar( myMenuBar );
if ( ( gAppleMenu = GetMHandle( APPLE_MENU_ID ) ) == NIL_POINTER )
ErrorHandler( NO_MENU );
AddResMenu( gAppleMenu, 'DRVR' );
if ( ( gFileMenu = GetMHandle( FILE_MENU_ID ) ) == NIL_POINTER )
ErrorHandler( NO_MENU );
if ( ( gEditMenu = GetMHandle( EDIT_MENU_ID ) ) == NIL_POINTER )
ErrorHandler( NO_MENU );
DrawMenuBar();
}
/******************************** MainLoop *********/
void MainLoop()
{
RgnHandle cursorRgn;
Boolean gotEvent;
gDone = FALSE;
gInBackground = FALSE;
cursorRgn = NewRgn();
gWNEImplemented = ( NGetTrapAddress( WNE_TRAP_NUM, ToolTrap ) !=
NGetTrapAddress( UNIMPL_TRAP_NUM, ToolTrap ) );
while ( gDone == FALSE )
{
if ( gWNEImplemented )
gotEvent = WaitNextEvent( everyEvent, &gTheEvent, MIN_SLEEP, cursorRgn );
else
{
SystemTask();
gotEvent = GetNextEvent( everyEvent, &gTheEvent );
}
AdjustCursor( gTheEvent.where, cursorRgn );
if ( gotEvent )
HandleEvent();
else
DoIdle();
}
}
/************************************* HandleEvent */
void HandleEvent()
{
char c;
switch ( gTheEvent.what )
{
case nullEvent:
DoIdle();
break;
case mouseDown:
HandleMouseDown();
break;
case keyDown:
case autoKey:
c = gTheEvent.message & charCodeMask;
if (( gTheEvent.modifiers & cmdKey ) != 0)
{
AdjustMenus();
HandleMenuChoice( MenuKey( c ) );
}
else
DoTEKey( c );
break;
case activateEvt:
DoActivate( (WindowPtr)gTheEvent.message, (gTheEvent.modifiers & activeFlag) != 0 );
break;
case updateEvt:
DoUpdate( (WindowPtr)gTheEvent.message );
break;
case app4Evt:
if ( ( gTheEvent.message & SUSPEND_RESUME_BIT ) == RESUMING )
{
gInBackground = (gTheEvent.message & 0x01) == 0;
DoActivate(FrontWindow(), !gInBackground);
}
else
DoIdle();
break;
}
}
/************************************* DoTEKey */
void DoTEKey( c )
char c;
{
WindowPtr window;
FormPeek form;
int wType, length, i;
CharsHandle text;
Str255 tempStr;
window = FrontWindow();
wType = WindowType( window );
if ( wType == FORM_WINDOW )
{
form = (FormPeek)window;
if ( c == TE_TAB_CHAR )
{
if ( form->curTE == form->nameTE )
SwitchToNewArea( form, TE_MISC_AREA );
else
{
SwitchToNewArea( form, TE_NAME_AREA );
TESetSelect( 0, 32767, form->curTE );
}
}
else
{
TEKey( c, form->curTE );
if ( form->curTE == form->nameTE )
{
length = (*form->nameTE)->teLength;
if ( length == 0 )
SetWTitle( window, UNTITLED_STRING );
else
{
text = TEGetText( form->nameTE );
tempStr[ 0 ] = length;
for ( i=0; ( (i<length) && (i<256) ); i++ )
{
tempStr[ i+1 ] = (*(char **)text)[ i ];
}
SetWTitle( window, tempStr );
}
}
else
AdjustScrollBar( form );
}
}
}
/************************************* DoIdle */
void DoIdle()
{
WindowPtr window;
int wType;
window = FrontWindow();
wType = WindowType( window );
if ( wType == FORM_WINDOW )
TEIdle( ((FormPeek)window)->curTE );
}
/************************************* HandleMouseDown */
void HandleMouseDown()
{
WindowPtr window;
short int thePart;
long int menuChoice, windSize;
thePart = FindWindow( gTheEvent.where, &window );
switch ( thePart )
{
case inMenuBar:
AdjustMenus();
menuChoice = MenuSelect( gTheEvent.where );
HandleMenuChoice( menuChoice );
break;
case inSysWindow:
SystemClick( &gTheEvent, window );
break;
case inContent:
if ( window != FrontWindow() )
{
SelectWindow(window);
}
else
DoContentClick( window, gTheEvent.where );
break;
case inDrag:
DragWindow( window, gTheEvent.where, &(screenBits.bounds) );
break;
case inGoAway:
if ( TrackGoAway(window, gTheEvent.where) )
DoCloseWindow( window );
break;
}
}
/************************************* DoCloseWindow */
void DoCloseWindow( window )
WindowPtr window;
{
HideWindow( window );
DisposeControl( ((FormPeek)window)->vScroll );
TEDispose( ((FormPeek)window)->nameTE );
TEDispose( ((FormPeek)window)->miscTE );
CloseWindow( window );
DisposPtr( (Ptr)window );
}
/************************************* AdjustMenus */
void AdjustMenus()
{
WindowPtr window;
int wType;
long offset;
TEHandle te;
window = FrontWindow();
wType = WindowType( window );
if ( window == NIL_POINTER )
{
DisableItem( gFileMenu, F_CLOSE_ITEM );
DisableItem( gEditMenu, E_UNDO_ITEM );
DisableItem( gEditMenu, E_CUT_ITEM );
DisableItem( gEditMenu, E_COPY_ITEM );
DisableItem( gEditMenu, E_PASTE_ITEM );
DisableItem( gEditMenu, E_CLEAR_ITEM );
}
else if ( wType == DA_WINDOW )
{
DisableItem( gFileMenu, F_CLOSE_ITEM );
EnableItem( gEditMenu, E_UNDO_ITEM );
EnableItem( gEditMenu, E_CUT_ITEM );
EnableItem( gEditMenu, E_COPY_ITEM );
EnableItem( gEditMenu, E_PASTE_ITEM );
EnableItem( gEditMenu, E_CLEAR_ITEM );
}
else if ( wType == FORM_WINDOW )
{
EnableItem( gFileMenu, F_CLOSE_ITEM );
DisableItem( gEditMenu, E_UNDO_ITEM );
DisableItem( gEditMenu, E_CUT_ITEM );
DisableItem( gEditMenu, E_COPY_ITEM );
DisableItem( gEditMenu, E_PASTE_ITEM );
DisableItem( gEditMenu, E_CLEAR_ITEM );
te = ((FormPeek)window)->curTE;
if ( (*te)->selStart < (*te)->selEnd )
{
EnableItem( gEditMenu, E_CUT_ITEM );
EnableItem( gEditMenu, E_COPY_ITEM );
EnableItem( gEditMenu, E_CLEAR_ITEM );
}
if ( GetScrap( NIL_POINTER, 'TEXT', &offset) > 0 )
EnableItem( gEditMenu, E_PASTE_ITEM );
}
}
/************************************* WindowType */
int WindowType( window )
WindowPtr window;
{
if ( window == NIL_POINTER )
return( NIL_WINDOW );
if ( ((WindowPeek)window)->windowKind < 0 )
return( DA_WINDOW );
if ( ((FormPeek)window)->wType == FORM_WINDOW )
return( FORM_WINDOW );
return( UNKNOWN_WINDOW );
}
/************************************* HandleMenuChoice */
void HandleMenuChoice( menuChoice )
long int menuChoice;
{
int theMenu;
int theItem;
if ( menuChoice != 0 )
{
theMenu = HiWord( menuChoice );
theItem = LoWord( menuChoice );
switch ( theMenu )
{
case APPLE_MENU_ID :
HandleAppleChoice( theItem );
break;
case FILE_MENU_ID :
HandleFileChoice( theItem );
break;
case EDIT_MENU_ID :
HandleEditChoice( theItem );
}
HiliteMenu( 0 );
}
}
/******************************** HandleAppleChoice *******/
void HandleAppleChoice( theItem )
int theItem;
{
Str255 accName;
int accNumber;
switch ( theItem )
{
case A_ABOUT_ITEM :
NoteAlert( ABOUT_ALERT, NIL_POINTER );
break;
default :
GetItem( gAppleMenu, theItem, accName );
accNumber = OpenDeskAcc( accName );
break;
}
}
/******************************** HandleFileChoice *******/
void HandleFileChoice( theItem )
int theItem;
{
WindowPtr window;
switch ( theItem )
{
case F_NEW_ITEM :
CreateWindow();
break;
case F_CLOSE_ITEM :
if ( ( window = FrontWindow() ) != NIL_POINTER )
DoCloseWindow( window );
break;
case F_QUIT_ITEM :
gDone = TRUE;
break;
}
}
/******************************** HandleEditChoice *******/
void HandleEditChoice( theItem )
int theItem;
{
TEHandle te;
WindowPtr window;
int wType, length, i;
CharsHandle text;
Str255 tempStr;
FormPeek form;
if ( ! SystemEdit( theItem - 1 ) )
{
window = FrontWindow();
wType = WindowType( window );
if ( wType == FORM_WINDOW )
{
form = (FormPeek)window;
te = form->curTE;
switch ( theItem )
{
case E_UNDO_ITEM:
break;
case E_CUT_ITEM:
if ( ZeroScrap() == noErr )
{
TECut(te);
AdjustScrollBar( form );
if ( TEToScrap() != noErr )
ZeroScrap();
}
break;
case E_COPY_ITEM:
if ( ZeroScrap() == noErr )
{
TECopy(te);
if ( TEToScrap() != noErr )
ZeroScrap();
}
break;
case E_PASTE_ITEM:
if ( TEFromScrap() == noErr )
{
TEPaste(te);
AdjustScrollBar( form );
}
break;
case E_CLEAR_ITEM:
TEDelete(te);
AdjustScrollBar( form );
break;
}
if ( te == form->nameTE )
{
length = (*form->nameTE)->teLength;
if ( length == 0 )
SetWTitle( window, UNTITLED_STRING );
else
{
text = TEGetText( form->nameTE );
tempStr[ 0 ] = length;
for ( i=0; ( (i<length) && (i<256) ); i++ )
{
tempStr[ i+1 ] = (*(char **)text)[ i ];
}
SetWTitle( window, tempStr );
}
}
}
}
}
/******************************** DoContentClick *******/
void DoContentClick( window, mouse )
WindowPtr window;
Point mouse;
{
int wType, value;
int thePart;
Boolean shiftDown;
Point locMouse;
ControlHandle control;
FormPeek form;
wType = WindowType( window );
if ( wType == FORM_WINDOW )
{
form = (FormPeek)window;
locMouse = mouse;
GlobalToLocal( &locMouse );
if ( ( thePart = FindControl( locMouse, window, &control ) ) != 0 )
{
switch( thePart )
{
case inUpButton:
case inDownButton:
case inPageUp:
case inPageDown:
value = TrackControl( control, locMouse, (ProcPtr) VActionProc );
break;
case inThumb:
value = GetCtlValue( control );
thePart = TrackControl( control, locMouse, NIL_POINTER );
if ( thePart != 0 )
{
value -= GetCtlValue( control );
if ( value != 0 )
TEScroll(0, value * (*form->curTE)->lineHeight, form->miscTE );
}
break;
}
}
else if ( PtInRect( locMouse, &gNameRect ) )
{
if ( form->curTE == form->nameTE )
{
shiftDown = ( gTheEvent.modifiers & shiftKey) != 0;
TEClick( locMouse, shiftDown, form->nameTE );
}
else
{
SwitchToNewArea( form, TE_NAME_AREA );
TEClick( locMouse, FALSE, form->nameTE );
}
}
else if ( PtInRect( locMouse, &gMiscRect ) )
{
if ( form->curTE == form->miscTE )
{
shiftDown = ( gTheEvent.modifiers & shiftKey) != 0;
TEClick( locMouse, shiftDown, form->miscTE );
}
else
{
SwitchToNewArea( form, TE_MISC_AREA );
TEClick( locMouse, FALSE, form->miscTE );
}
}
}
}
/************************************* VActionProc */
pascal void VActionProc(control, part)
ControlHandle control;
int part;
{
short amount;
WindowPtr window;
TEPtr te;
if ( part != 0 )
{
window = (*control)->contrlOwner;
te = *((FormPeek)window)->miscTE;
switch ( part ) {
case inUpButton:
case inDownButton: /* one line */
amount = 1;
break;
case inPageUp: /* one page */
case inPageDown:
amount = (te->viewRect.bottom - te->viewRect.top) / te->lineHeight;
break;
}
if ( (part == inDownButton) || (part == inPageDown) )
amount = -amount;
CommonAction(control, &amount);
if ( amount != 0 )
TEScroll( 0, amount * te->lineHeight, ((FormPeek)window)->miscTE );
}
}
/************************************* CommonAction */
void CommonAction( control, amount )
ControlHandle control;
short *amount;
{
short value, max;
value = GetCtlValue( control ); /* get current value */
max = GetCtlMax( control ); /* and maximum value */
*amount = value - *amount;
if ( *amount < 0 )
*amount = 0;
else if ( *amount > max )
*amount = max;
SetCtlValue( control, *amount );
*amount = value - *amount; /* calculate the real change */
}
/************************************ DoActivate */
void DoActivate( window, becomingActive )
WindowPtr window;
Boolean becomingActive;
{
FormPeek form;
int wType;
wType = WindowType( window );
if ( wType == FORM_WINDOW )
{
form = (FormPeek)window;
if ( becomingActive )
{
SetPort( window );
if ( form->curTE == form->miscTE )
TurnOnTextArea( form, TE_MISC_AREA );
else
TurnOnTextArea( form, TE_NAME_AREA );
HiliteControl( form->vScroll, 0 );
}
else
{
if ( form->curTE == form->miscTE )
TurnOffTextArea( form, TE_MISC_AREA );
else
TurnOffTextArea( form, TE_NAME_AREA );
HiliteControl( form->vScroll, 255 );
}
}
}
/******************************** AdjustCursor *********/
void AdjustCursor( mouse, region )
Point mouse;
RgnHandle region;
{
WindowPtr window;
RgnHandle arrowRgn, iBeamRgn, tempRgn;
Rect tempRect;
int wType;
GrafPtr oldPort;
window = FrontWindow();
wType = WindowType( window );
if ( gInBackground || ( wType != FORM_WINDOW ) )
{
SetCursor( &arrow );
return;
}
GetPort( &oldPort );
SetPort( window );
arrowRgn = NewRgn();
iBeamRgn = NewRgn();
tempRgn = NewRgn();
SetRectRgn( arrowRgn, -32700, -32700, 32700, 32700 );
tempRect = gNameRect;
LocalToGlobal( &TopLeft(tempRect) );
LocalToGlobal( &BotRight(tempRect) );
RectRgn( tempRgn, &tempRect );
UnionRgn( iBeamRgn, tempRgn, iBeamRgn );
tempRect = gMiscRect;
LocalToGlobal( &TopLeft(tempRect) );
LocalToGlobal( &BotRight(tempRect) );
RectRgn( tempRgn, &tempRect );
UnionRgn( iBeamRgn, tempRgn, iBeamRgn );
DiffRgn( arrowRgn, iBeamRgn, arrowRgn );
if ( PtInRgn( mouse, iBeamRgn ) )
{
SetCursor( *GetCursor( iBeamCursor ) );
CopyRgn( iBeamRgn, region );
}
else
{
SetCursor( &arrow );
CopyRgn( arrowRgn, region );
}
DisposeRgn( arrowRgn );
DisposeRgn( iBeamRgn );
DisposeRgn( tempRgn );
SetPort( oldPort );
}
/************************************ DoUpdate */
void DoUpdate( window )
WindowPtr window;
{
FormPeek form;
int wType;
GrafPtr oldPort;
GetPort( &oldPort );
SetPort( window );
wType = WindowType( window );
if ( wType == FORM_WINDOW )
{
BeginUpdate( window );
EraseRect( &window->portRect );
DrawForm( window );
EndUpdate( window );
}
SetPort( oldPort );
}
/************************************ DrawForm */
void DrawForm( window )
WindowPtr window;
{
FrameRect( &gNameRect );
FrameRect( &gMiscRect );
DrawControls( window );
TextFont( geneva );
TextFace( bold );
MoveTo( gNameRect.left - 34, gNameRect.top + 12 );
DrawString( "\pName" );
MoveTo( gMiscRect.left - 34, gMiscRect.top + 12 );
DrawString( "\pMisc." );
TextFont( monaco );
TextFace( 0 );
TEUpdate( &window->portRect, ((FormPeek)window)->nameTE );
TEUpdate( &window->portRect, ((FormPeek)window)->miscTE );
}
/************************************ CreateWindow */
void CreateWindow()
{
WindowPtr theNewestWindow;
Ptr wStorage;
FormPeek form;
wStorage = NewPtr( sizeof(FormRecord) );
if ( ( theNewestWindow = GetNewWindow( BASE_RES_ID, wStorage,
MOVE_TO_FRONT ) ) == NIL_POINTER )
ErrorHandler( NO_WIND );
if ( ( (screenBits.bounds.right - gNewWindowLeft) < DRAG_THRESHOLD ) ||
( ( screenBits.bounds.bottom - gNewWindowTop) < DRAG_THRESHOLD ) )
{
gNewWindowLeft = WINDOW_HOME_LEFT;
gNewWindowTop = WINDOW_HOME_TOP;
}
MoveWindow( theNewestWindow, gNewWindowLeft, gNewWindowTop, LEAVE_WHERE_IT_IS );
gNewWindowLeft += NEW_WINDOW_OFFSET;
gNewWindowTop += NEW_WINDOW_OFFSET;
form = (FormPeek)theNewestWindow;
form->wType = FORM_WINDOW;
form->vScroll = NewControl( theNewestWindow, &gScrollBarRect, NIL_STRING,
VISIBLE, 0, 0, 0, scrollBarProc, 0L);
ShowWindow( theNewestWindow );
SetPort( theNewestWindow );
TextFont( monaco );
TextFace( 0 );
TextSize( 9 );
StartTextEdit( form );
}
/******************************** StartTextEdit *********/
void StartTextEdit( form )
FormPeek form;
{
Rect r;
r = gNameRect;
InsetRect( &r, 2, 2 );
form->nameTE = TENew( &r, &r );
r = gMiscRect;
InsetRect( &r, 2, 2 );
form->miscTE = TENew( &r, &r );
SetClikLoop( NewClikLoop, form->miscTE );
TEAutoView( TRUE, form->miscTE );
form->curTE = form->nameTE;
}
/******************************** NewClikLoop *********/
pascal Boolean NewClikLoop()
{
WindowPtr window;
FormPeek form;
TEHandle te;
Rect tempRect;
Point mouse;
GrafPtr oldPort;
short amount;
RgnHandle oldClip;
window = FrontWindow();
if ( WindowType( window ) != FORM_WINDOW )
return( FALSE );
form = (FormPeek)window;
te = form->curTE;
GetPort( &oldPort );
SetPort( window );
oldClip = NewRgn();
GetClip( oldClip );
SetRect( &tempRect, -32767, -32767, 32767, 32767 );
ClipRect( &tempRect );
GetMouse( &mouse );
if ( mouse.v < gMiscRect.top )
{
amount = 1;
CommonAction( form->vScroll, &amount );
if ( amount != 0 )
TEScroll( 0, amount * ((*te)->lineHeight), te );
}
else if ( mouse.v > gMiscRect.bottom )
{
amount = -1;
CommonAction( form->vScroll, &amount );
if ( amount != 0 )
TEScroll( 0, amount * ((*te)->lineHeight), te );
}
SetClip( oldClip );
DisposeRgn( oldClip );
SetPort( oldPort );
return( TRUE );
}
/******************************** SwitchToNewArea *********/
void SwitchToNewArea( form, newArea )
FormPeek form;
int newArea;
{
if ( form->curTE == form->nameTE )
{
TurnOffTextArea( form, TE_NAME_AREA );
TurnOnTextArea( form, TE_MISC_AREA );
}
else
{
TurnOffTextArea( form, TE_MISC_AREA );
TurnOnTextArea( form, TE_NAME_AREA );
}
}
/******************************** TurnOnTextArea *********/
void TurnOnTextArea( form, whichArea )
FormPeek form;
int whichArea;
{
TEPtr te;
if ( whichArea == TE_MISC_AREA )
{
te = *form->miscTE;
te->viewRect.bottom = (((te->viewRect.bottom - te->viewRect.top) / te->lineHeight)
* te->lineHeight) + te->viewRect.top;
te->destRect.bottom = te->viewRect.bottom;
AdjustScrollBar( form );
form->curTE = form->miscTE;
}
else
form->curTE = form->nameTE;
TEActivate( form->curTE );
}
/******************************** TurnOffTextArea *********/
void TurnOffTextArea( form, whichArea )
FormPeek form;
int whichArea;
{
if ( whichArea == TE_MISC_AREA )
TEDeactivate( form->miscTE );
else
TEDeactivate( form->nameTE );
}
/******************************** AdjustScrollBar *********/
void AdjustScrollBar( form )
FormPeek form;
{
short value, lines, max;
short oldValue, oldMax;
TEPtr te;
oldValue = GetCtlValue( form->vScroll );
oldMax = GetCtlMax( form->vScroll );
te = *(form->miscTE);
lines = te->nLines;
if ( *(*te->hText + te->teLength - 1) == TE_CARRIAGE_RETURN )
lines += 1;
max = lines - ((te->viewRect.bottom - te->viewRect.top) /
te->lineHeight);
if ( max < 0 ) max = 0;
SetCtlMax( form->vScroll, max);
te = *(form->miscTE);
value = (te->viewRect.top - te->destRect.top) / te->lineHeight;
if ( value < 0 ) value = 0;
else if ( value > max ) value = max;
SetCtlValue( form->vScroll, value);
TEScroll( 0, (te->viewRect.top - te->destRect.top) -
(GetCtlValue( form->vScroll ) * te->lineHeight), form->miscTE );
}
/******************************** ErrorHandler *********/
void ErrorHandler( stringNum )
int stringNum;
{
StringHandle errorStringH;
if ( ( errorStringH = GetString( stringNum ) ) == NIL_POINTER )
ParamText( HOPELESSLY_FATAL_ERROR, NIL_STRING, NIL_STRING, NIL_STRING );
else
{
HLock( (Handle)errorStringH );
ParamText( *errorStringH, NIL_STRING, NIL_STRING, NIL_STRING );
HUnlock( (Handle)errorStringH );
}
StopAlert( ERROR_ALERT_ID, NIL_POINTER );
ExitToShell();
}